package com.ruihang.generalibrary.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.os.Build;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;

import com.ruihang.generalibrary.R;


/**
 * Created by Ly on 2016/11/25.
 */
public class CustomEXImageView extends AppCompatImageView {

	private float width, height;
	private int defaultRadius = 0;
	private int radius;
	private int leftTopRadius;
	private int rightTopRadius;
	private int rightBottomRadius;
	private int leftBottomRadius;

	public interface IRatio {
		float getWidthRatio();

		float getHeightRatio();
	}

	private class Ratio16_9 implements IRatio {
		@Override
		public float getWidthRatio() {
			return 16;
		}

		@Override
		public float getHeightRatio() {
			return 9;
		}
	}

	private class Ratio4_3 implements IRatio {
		@Override
		public float getWidthRatio() {
			return 4;
		}

		@Override
		public float getHeightRatio() {
			return 3;
		}
	}

	private class Ratio3_4 implements IRatio {
		@Override
		public float getWidthRatio() {
			return 3;
		}

		@Override
		public float getHeightRatio() {
			return 4;
		}
	}

	private class Ratio1_1 implements IRatio {
		@Override
		public float getWidthRatio() {
			return 1;
		}

		@Override
		public float getHeightRatio() {
			return 1;
		}
	}

	private class Ratio8_3 implements IRatio {
		@Override
		public float getWidthRatio() {
			return 8;
		}

		@Override
		public float getHeightRatio() {
			return 3;
		}
	}

	protected IRatio ratio;

	protected int ratioType;

	public CustomEXImageView(Context context) {
		super(context);
		init(context, null);
	}

	public CustomEXImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		init(context, attrs);
	}

	public CustomEXImageView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initCustomAttr(context, attrs, defStyleAttr);
		init(context, attrs);
	}
//    public CustomEXImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        initCustomAttr(context,attrs, defStyleAttr);
//    }

	private void initCustomAttr(Context context, AttributeSet attrs, int defStyle) {
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomEXViewAttr, defStyle, 0);
		int ratioType = a.getInt(R.styleable.CustomEXViewAttr_ratioType, 0);
		if (ratioType > 0) {
			setRatioType(ratioType);
		} else {
			float rw = a.getFloat(R.styleable.CustomEXViewAttr_ratioWidth, 0);
			float rh = a.getFloat(R.styleable.CustomEXViewAttr_ratioHeight, 0);
			if (rw > 0 && rh > 0) {
				setOuterRatioType(-1, new InternalRatio(rw, rh));
			}
		}
		a.recycle();
	}


	private void init(Context context, AttributeSet attrs) {
		if (Build.VERSION.SDK_INT < 18) {
			setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
		// 读取配置
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
		radius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_radius, defaultRadius);
		leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_top_radius, defaultRadius);
		rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_top_radius, defaultRadius);
		rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_bottom_radius, defaultRadius);
		leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_bottom_radius, defaultRadius);


		//如果四个角的值没有设置，那么就使用通用的radius的值。
		if (defaultRadius == leftTopRadius) {
			leftTopRadius = radius;
		}
		if (defaultRadius == rightTopRadius) {
			rightTopRadius = radius;
		}
		if (defaultRadius == rightBottomRadius) {
			rightBottomRadius = radius;
		}
		if (defaultRadius == leftBottomRadius) {
			leftBottomRadius = radius;
		}
		array.recycle();
	}


	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		width = getWidth();
		height = getHeight();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//这里做下判断，只有图片的宽高大于设置的圆角距离的时候才进行裁剪
		int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
		int maxRight = Math.max(rightTopRadius, rightBottomRadius);
		int minWidth = maxLeft + maxRight;
		int maxTop = Math.max(leftTopRadius, rightTopRadius);
		int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
		int minHeight = maxTop + maxBottom;
		if (width >= minWidth && height > minHeight) {
			Path path = new Path();
			//四个角：右上，右下，左下，左上
			path.moveTo(leftTopRadius, 0);
			path.lineTo(width - rightTopRadius, 0);
			path.quadTo(width, 0, width, rightTopRadius);

			path.lineTo(width, height - rightBottomRadius);
			path.quadTo(width, height, width - rightBottomRadius, height);

			path.lineTo(leftBottomRadius, height);
			path.quadTo(0, height, 0, height - leftBottomRadius);

			path.lineTo(0, leftTopRadius);
			path.quadTo(0, 0, leftTopRadius, 0);

			canvas.clipPath(path);
		}
		super.onDraw(canvas);
	}





	public void setRatioType(int ratioType) {
		if (this.ratioType == ratioType) {
			return;
		}
		switch (ratioType) {
			case 1:
				ratio = new Ratio16_9();
				break;
			case 2:
				ratio = new Ratio3_4();
				break;
			case 3:
				ratio = new Ratio4_3();
				break;
			case 4:
				ratio = new Ratio1_1();
				break;
			case 5:
				ratio = new Ratio8_3();
				break;
			default:
				ratio = null;
				break;
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (ratio != null) {
			int widthSize = getMeasuredWidth();
			int heightSize = getMeasuredHeight();
			float w = widthSize;
			if (Math.abs(ratio.getWidthRatio() / ratio.getHeightRatio() - w / heightSize) > 0.003) {
				heightSize = (int) (w * ratio.getHeightRatio() / ratio.getWidthRatio());
			}

			setMeasuredDimension(widthSize, heightSize);
		}
	}

	public void setOuterRatioType(int type, IRatio ratio) {
		if (ratio == null) {
			return;
		}
		this.ratioType = type;
		this.ratio = ratio;
	}

	public IRatio getRatio() {
		return ratio;
	}

	public static class InternalRatio implements IRatio {

		private float rw;
		private float rh;

		public InternalRatio(float rw, float rh) {
			this.rw = rw;
			this.rh = rh;
		}

		@Override
		public float getWidthRatio() {
			return rw;
		}

		@Override
		public float getHeightRatio() {
			return rh;
		}

		public float getRw() {
			return rw;
		}

		public float getRh() {
			return rh;
		}

		public void setRatios(float rw, float rh) {
			this.rw = rw;
			this.rh = rh;
		}
	}
}
